// pages/search/search.js
import { request } from '../../request/index'
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue:'',
    goodsArr:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.sendRequest = this.debounce(this.getGoodsArr)
  },

  bindinput(event) {
    console.log(event)
    const { value } = event.detail
    if(!value.trim()){
      return
    } 
    this.sendRequest(value)
  },
  //发送请求获取数据
  async getGoodsArr(value) {
    const result = await request({url:'/goods/qsearch',data:{query:value}})
    this.setData({goodsArr:result.data.message})
  },
  //防抖函数value
  debounce(callback, delay = 1000){
    let timer = null
    return (value) => {
      clearInterval(timer)
      timer = setTimeout( () => {
        this.setData({searchValue:value})
        callback(value)
      },delay)
    }
  },
  //清空搜索框
  clearInput(){
    this.setData({goodsArr:[],searchValue:''})
  },

  //去到详情页面
  goDetailPage(event){
    const {id} = event.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/goods_detail/goods_detail?id=' + id
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },


})